//
//  DetailListViewController.m
//  MapCallouts
//
//  Created by Hideo Tomizu on 10/08/30.
//  Copyright 2010 株式会社ビットウェイ. All rights reserved.
//

#import "DetailListViewController.h"
#import "DamInfo.h"
#import "AppDelegate.h"
#import "DamTableViewCell.h"

@implementation DetailListViewController

@synthesize dam_cd;
@synthesize damInfo;


#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	
	[self setDamInfo];
}

- (void) setDamInfo {
	NSLog(@"%d:damcd",dam_cd);
	AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
	self.damInfo = [DamInfo getDamInfoDetail:[appDelegate getDBPath] damCd:dam_cd];
	self.title = self.damInfo.dam_name;
}

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 12;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    /*
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
	*/
	switch(indexPath.row){
		case 0: {
			UITableViewCell *topCell = [tableView dequeueReusableCellWithIdentifier:@"topCell"];
			if(topCell==nil){
				topCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"topCell"];
			}
			topCell.textLabel.text = damInfo.dam_name;
			return topCell;
			break;
		}
		case 1:{
			/*
			DamTableViewCell *cell = (DamTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"adCell"];
			if(cell==nil){
				UIViewController *viewController;
				viewController = [[UIViewController alloc]initWithNibName:@"DamTableViewCell" bundle:nil];
				cell = (DamTableViewCell *)viewController.view;
				[viewController release];
			}
			*/
			DamTableViewCell *cell = [self tableView:tableView getDamCellView:@"test"];
			cell.titleLabel.text = @"住所";
			[cell.detailLabel setLineBreakMode:UILineBreakModeWordWrap];//改行モード
			[cell.detailLabel setNumberOfLines:2];
			cell.detailLabel.text = [NSString stringWithFormat:@"%@%@",damInfo.ken_name, damInfo.address];
			/*
			UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"adCell"];
			if(cell==nil){
				cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"adCell"];
			}
			cell.textLabel.text = @"住所";
			cell.detailTextLabel.text = [NSString stringWithFormat:@"%@%@",damInfo.ken_name, damInfo.address];
			 */
			return cell;
			break;
		}
		case 2:{
			/*
			DamTableViewCell *cell = (DamTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"adCell"];
			if(cell==nil){
				UIViewController *viewController;
				viewController = [[UIViewController alloc]initWithNibName:@"DamTableViewCell" bundle:nil];
				cell = (DamTableViewCell *)viewController.view;
				[viewController release];
			}
			*/
			DamTableViewCell *cell = [self tableView:tableView getDamCellView:@"test"];
			cell.titleLabel.text = @"水系名";
			[cell.detailLabel setLineBreakMode:UILineBreakModeWordWrap];//改行モード
			[cell.detailLabel setNumberOfLines:2];
			cell.detailLabel.text = damInfo.water_name;
			/*
			UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"typeCell"];
			if(cell==nil){
				cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"typeCell"];
			}
			cell.textLabel.text = @"ダム形式";
			cell.detailTextLabel.text = damInfo.type_name;
			 */
			return cell;
			break;
		}
		case 3:{
			DamTableViewCell *cell = [self tableView:tableView getDamCellView:@"test"];
			cell.titleLabel.text = @"河川名";
			[cell.detailLabel setLineBreakMode:UILineBreakModeWordWrap];//改行モード
			[cell.detailLabel setNumberOfLines:2];
			cell.detailLabel.text = damInfo.river_name;
			return cell;
			break;
		}
		case 4:{
			DamTableViewCell *cell = [self tableView:tableView getDamCellView:@"test"];
			cell.titleLabel.text = @"形式";
			[cell.detailLabel setLineBreakMode:UILineBreakModeWordWrap];//改行モード
			[cell.detailLabel setNumberOfLines:2];
			cell.detailLabel.text = damInfo.type_name;
			return cell;
			break;
		}
		case 5:{
			DamTableViewCell *cell = [self tableView:tableView getDamCellView:@"test"];
			cell.titleLabel.text = @"目的";
			[cell.detailLabel setLineBreakMode:UILineBreakModeWordWrap];//改行モード
			[cell.detailLabel setNumberOfLines:2];
			cell.detailLabel.text = damInfo.purpose_name;
			return cell;
			break;
		}
		case 6:{
			DamTableViewCell *cell = [self tableView:tableView getDamCellView:@"test"];
			[cell.titleLabel setLineBreakMode:UILineBreakModeWordWrap];//改行モード
			[cell.titleLabel setNumberOfLines:2];
			cell.titleLabel.text = @"ダム規模\n(堤高)";
			[cell.detailLabel setLineBreakMode:UILineBreakModeWordWrap];//改行モード
			[cell.detailLabel setNumberOfLines:2];
			if([damInfo.height intValue] != 0) {
				cell.detailLabel.text = [NSString stringWithFormat:@"%.1fm",[damInfo.height floatValue]];
			}else{
				cell.detailLabel.text = @"(データ無し)";
			}
			return cell;
			break;
		}
		case 7:{
			DamTableViewCell *cell = [self tableView:tableView getDamCellView:@"test"];
			[cell.titleLabel setLineBreakMode:UILineBreakModeWordWrap];//改行モード
			[cell.titleLabel setNumberOfLines:2];
			cell.titleLabel.text = @"ダム規模\n(堤頂長)";
			[cell.detailLabel setLineBreakMode:UILineBreakModeWordWrap];//改行モード
			[cell.detailLabel setNumberOfLines:2];
			if([damInfo.width intValue] != 0){
				cell.detailLabel.text = [NSString stringWithFormat:@"%.1fm",[damInfo.width floatValue]];
			}else{
				cell.detailLabel.text = @"(データ無し)";
			}
			return cell;
			break;
		}
		case 8:{
			DamTableViewCell *cell = [self tableView:tableView getDamCellView:@"test"];
			cell.titleLabel.text = @"堤体積";
			[cell.detailLabel setLineBreakMode:UILineBreakModeWordWrap];//改行モード
			[cell.detailLabel setNumberOfLines:2];
			if(damInfo.volume!=0){
				cell.detailLabel.text = [NSString stringWithFormat:@"%d㎥",damInfo.volume];
			}else{
				cell.detailLabel.text = @"(データ無し)";
			}
			return cell;
			break;
		}
		case 9:{
			DamTableViewCell *cell = [self tableView:tableView getDamCellView:@"test"];
			cell.titleLabel.text = @"総貯水量";
			[cell.detailLabel setLineBreakMode:UILineBreakModeWordWrap];//改行モード
			[cell.detailLabel setNumberOfLines:2];
			if(damInfo.pondage!=0){
				cell.detailLabel.text = [NSString stringWithFormat:@"%d㎥",damInfo.pondage];
			}else{
				cell.detailLabel.text = @"(データ無し)";
			}
			return cell;
			break;
		}
		case 10:{
			DamTableViewCell *cell = [self tableView:tableView getDamCellView:@"test"];
			cell.titleLabel.text = @"ダム事業者";
			[cell.detailLabel setLineBreakMode:UILineBreakModeWordWrap];//改行モード
			[cell.detailLabel setNumberOfLines:2];
			cell.detailLabel.text = damInfo.institution_name;
			return cell;
			break;
		}
		case 11:{
			DamTableViewCell *cell = [self tableView:tableView getDamCellView:@"test"];
			cell.titleLabel.text = @"竣工年";
			[cell.detailLabel setLineBreakMode:UILineBreakModeWordWrap];//改行モード
			[cell.detailLabel setNumberOfLines:2];
			if(damInfo.const_year!=9999){
				cell.detailLabel.text = [NSString stringWithFormat:@"%d年",damInfo.const_year];
			}else{
				cell.detailLabel.text = @"(データ無し)";
			}
			return cell;
			break;
		}
			/*
			水系名 	water_name
			河川名 	river_name
			形式コード	type_cd
			目的コード	purpose_cd
			ダム規模(堤高) 	height
			ダム規模（堤頂長） 	width
			堤体積 	volume
			総貯水量 	pondage
			ダム事業者コード	institution_cd
			竣工年 	const_year
			*/
			
			
		default : {
			break;
		}
	}
	

    
    // Configure the cell...
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    return cell;
}

-(id) tableView:(UITableView *)tableView getDamCellView:(NSString *)ident
{
	DamTableViewCell *cell = (DamTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ident];
	if(cell==nil){
		UIViewController *viewController;
		viewController = [[UIViewController alloc]initWithNibName:@"DamTableViewCell" bundle:nil];
		cell = (DamTableViewCell *)viewController.view;
		[viewController release];
	}
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	return cell;
}

-( CGFloat ) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	switch( indexPath.row )
	{
		default: return 40; 
		//case 0: return 40;
		//case 1: return 60;
		//case 2: return 90;
	}
	return 40;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	/*
	 <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
	 [self.navigationController pushViewController:detailViewController animated:YES];
	 [detailViewController release];
	 */
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

